package com.hotel.front;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.StatusTitle;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.ChargeLogic;
import com.hotel.BL.GenericLogic;
import com.hotel.BL.NightAuditLogic;
import com.hotel.BL.ReservationLogic;
import com.hotel.BL.RoomLogic;
import com.hotel.BL.UserLogic;

import data.Charge;
import data.General;
import data.Reservation;
import data.Room;
import data.passingData.AccDetailsData;
import data.passingData.CounterData;
import data.passingData.NightAuditReportResData;
import data.passingData.SummaryNightAuditData;
import data.passingData.UserLogedinData;

@SuppressWarnings("serial")
public class NightAuditService extends HttpServlet{
	//localhost:8080/nightauditservice?func=activenightaudit
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try{
			String doClass = req.getParameter("func");
			if(doClass.equals("activenightaudit")){
				NightAudit(req,resp);
			}else if(doClass.equals("changetotrue")){
				ChangeToTrue(req,resp);
			}else if(doClass.equals("changetofalse")){
				ChangeToFalse(req,resp);
			}else if(doClass.equals("loadsummary")){
				SummaryNightAudit(req,resp);
			}else if(doClass.equals("inprogressnightaudit")){
				InProgress(req,resp);
			}else if(doClass.equals("getlogedinuser")){
				GetLogedInUser(req,resp);
			}else if(doClass.equals("getNightAuditReport")){
				GetNightAuditReport(req,resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void NightAudit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException, ClassNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		NightAuditLogic nightAuditLogic = new NightAuditLogic(req,resp);
		Calendar cal = Calendar.getInstance();
		//cal.add(Calendar.DATE, -1);
		cal.set(Calendar.HOUR_OF_DAY,0);
		cal.set(Calendar.MINUTE,0);
		cal.set(Calendar.SECOND,0);
		cal.set(Calendar.MILLISECOND,0);
		Date nauditDate = cal.getTime();
		nightAuditLogic.ChangeIsTempCharge(hotelId, nauditDate);
		nightAuditLogic.SaveSerialize(hotelId);
		GenericLogic genericLogic = new GenericLogic(req,resp);
		General g = genericLogic.GetGeneral(hotelId);
		long occupied = g.getOnNightAuditRollOverMarkOccupied().getId();
		long vacant = g.getOnNightAuditRollOverMarkVacantRoom().getId();
		long checkout = g.getMarkCheckedOutRoom().getId();
		long DNRid = genericLogic.GetCleaningStatusByTitle(
				hotelId, StatusTitle.CleaningStatusTitle.DNR).getId();
		long inspect = genericLogic.GetCleaningStatusByTitle(
				hotelId, StatusTitle.CleaningStatusTitle.InspectRooms).getId();
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		RoomLogic roomLogic = new RoomLogic(req,resp);
		List<Reservation> occupiedroom = resLogic.GetStayToday(hotelId);
		List<Reservation> checkoutroom = resLogic.GetCheckoutToday(hotelId);
		List<Reservation> dnrtoday = resLogic.GetDNRToday(hotelId);
		List<Reservation> dnryesterday = resLogic.GetDNRYesterday(hotelId);
		List<Room> allRoom = roomLogic.GetAllRoom(hotelId);
		for(int i=0;i<allRoom.size();i++){
			roomLogic.ChangeCleaningStatus(allRoom.get(i).getId(), vacant);
		}
		nightAuditLogic.ChangeCleaningStatusAllRoom(occupied,occupiedroom);
		nightAuditLogic.ChangeCleaningStatusAllRoom(checkout,checkoutroom);
		nightAuditLogic.ChangeCleaningStatusAllRoom(DNRid,dnrtoday);
		nightAuditLogic.ChangeCleaningStatusAllRoom(inspect,dnryesterday);
				
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(null));
	}
	
	private void ChangeToTrue(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		ChargeLogic chargeLogic = new ChargeLogic(req,resp);
		List<Charge> chargelist = chargeLogic.GetAllCharge(hotelId);
		for(int i =0;i<chargelist.size();i++){
			chargelist.get(i).setTemp(true);
			chargeLogic.UpdateCharge(chargelist.get(i));
		}
		Object obj = null;
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeToFalse(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		ChargeLogic chargeLogic = new ChargeLogic(req,resp);
		List<Charge> chargelist = chargeLogic.GetAllCharge(hotelId);
		for(int i =0;i<chargelist.size();i++){
			chargelist.get(i).setTemp(false);
			chargeLogic.UpdateCharge(chargelist.get(i));
		}
		Object obj = null;
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void SummaryNightAudit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		NightAuditLogic nightAuditLogic = new NightAuditLogic(req,resp);
		SummaryNightAuditData sumData = nightAuditLogic.GetSummaryData(hotelId);
		List<AccDetailsData> accData = nightAuditLogic.GetAccDetails(hotelId);
		//TODO: �� Tax mapping
		//RoomTaxData roomTaxData = new NightAuditLogic().GetRoomTaxData(hotelId);
		//PosTaxData posData = new NightAuditLogic().GetPOSTaxData(hotelId);
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("sumData", sumData);
		map.put("accData", accData);
		map.put("roomTax", null);
		map.put("posTax", null);
		//map.put("date",  new GenericLogic().ServerDateFromNightAudit(hotelId));
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(map));
	}
	private void InProgress(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		General g = new GenericLogic(req,resp).GetGeneral(hotelId);
		String result = "F";
		if(g.isInprogress()){
			result = "T";
		}
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(result));
	}
	private void GetLogedInUser(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<UserLogedinData> userlist = new UserLogic(req,resp).GetLogedInUser(hotelId);
		GenericLogic gLogic = new GenericLogic(req,resp);
		General g = gLogic.GetGeneral(hotelId);
		g.setIncloudetax(true);
		gLogic.UpdateGenerlic(g);
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("data", userlist);
		map.put("date", gLogic.ServerDateFromNightAudit(hotelId));
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(map));
	}
	
	private void GetNightAuditReport(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		ReservationLogic resLogic = new ReservationLogic(req,resp);
		NightAuditLogic nightAuditLogic = new NightAuditLogic(req,resp);
		List<Reservation> resCheckIn = resLogic.GetCheckedinToday(hotelId);
		List<NightAuditReportResData> resData = resLogic.GetResCheckInToDay(hotelId);
		List<NightAuditReportResData> guestCheckIn = nightAuditLogic.GetGuestCheckInToDay(resCheckIn);

		List<CounterData> counter = nightAuditLogic.CounterToDay(hotelId);
		
		HashMap<String,Object> map = new HashMap<String,Object>();
		map.put("res", resData);
		map.put("guest", guestCheckIn);
		map.put("counter", counter);
		map.put("date", new GenericLogic(req,resp).ServerDateFromNightAudit(hotelId));
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(map));
	}
}
